package com.wuercloud.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableWebMvc
@EnableSwagger2 // 启用Swagger2
@Configuration // 让Spring来加载该类配置
public class SwaggerConfig extends WebMvcConfigurerAdapter {
	@Bean
	public Docket buildDocket() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(buildApiInf())
				.select()//选择哪些路径和API会生成document 
				.apis(RequestHandlerSelectors.basePackage("com.wuercloud.controller"))// 对所有的API监控
				.paths(PathSelectors.any())// 对所有的路径监控
				.build();
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html")
		.addResourceLocations("classpath:/META-INF/resources/");

		registry.addResourceHandler("/webjars/**")
		.addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	private ApiInfo buildApiInf() {
		return new ApiInfoBuilder()
				.title("信息管理系统接口API")
				.termsOfServiceUrl("https://my.oschina.net/u/3420056/blog")
				.description("ChianSun")
				.contact(new Contact("ChianSun", "ChianSun", "ChinaSun02@gmail.com"))
				.version("1.0")
				.build();
	}
}
